/**
 * 
 */

package com.wutianyi.arithmetic.ssl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;

/**
 * 使用安全套接字和安全传输性
 * 
 * @author wutianyi
 * 
 */
public class HttpsServerExample {

	public static void main(String[] args) {
		
		SSLServerSocketFactory factory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
		try {
			SSLServerSocket ss = (SSLServerSocket)factory.createServerSocket(8099);
			ss.setNeedClientAuth(true);
			while(true) {
				Socket s = ss.accept();
				System.out.println("Client connection made");
				BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
				System.out.println(in.readLine());
				PrintWriter pw = new PrintWriter(s.getOutputStream());
				pw.println("<html><head><title>Https server example</title></head><body><h1>Hello World!</h1></body></html>");
				pw.close();
				s.close();
			}
		}catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
